package homework;

import java.util.HashMap;

public class Test04_StringWork {
    public static void main(String[] args) {
        //统计字符串中字符出现的次数 abcdadfkjla(选做)
        HashMap<Character,Integer> hashMap = new HashMap<Character,Integer>();
        String s = "abcdadfkjla";
        for(int i=0;i<s.length();i++){
            if(hashMap.containsKey(s.charAt(i))){
                hashMap.put(s.charAt(i),hashMap.get(s.charAt(i))+1);
            }else{
                hashMap.put(s.charAt(i),1);
            }
        }
        System.out.println(hashMap);
        //将字符串翻转 abc->cba
        String b = "abc";
        String c = "";
        for(int i=b.length()-1;i>=0;i--){
            c = c+b.charAt(i);
        }
        System.out.println(c);
        StringBuffer e = new StringBuffer();
        for(int i=b.length()-1;i>=0;i--){
            e.append(b.charAt(i));
        }
        System.out.println(e);
        //字符串的翻转
        StringBuffer d = new StringBuffer(b);
        d.reverse();
        System.out.println(d);
        //将单词翻转 i am a student -> student a am i
        String h = "i am a student";
        String[] s1 = h.split(" ");
        StringBuffer s2 = new StringBuffer();
        for(int i=s1.length-1;i>=0;i--){
            s2.append(s1[i]);
            s2.append(" ");
        }
        //删除StringBuffer的最后一个元素
        s2.delete(s2.length()-1,s2.length());
        //s2.deleteCharAt()
        //转成字符串类型 使用trim删除两端空格
        h = s2.toString().trim();
        System.out.println(h);
        System.out.println(s2);
    }
}
